package com.uroad.lib.util.check;

import com.uroad.lib.util.string.StringUtils;

/**
 * Created by hrd on 2018/1/26 0026.
 */

public class VersonUtil {
    /**
     * 检查当前版本是否小于最新版本 if(currVerson < lastVerson) return true;
     * currVerson APP当前版本       1.6.1
     * lastVerson 接口返回的最新版本 1.7.8
     * */
    public static boolean isNeedUpdate(String currVerson,String lastVerson){
        try{
            boolean result = false;
            String[] currVerson_array = currVerson.split("\\.");
            String[] lastVerson_array = lastVerson.split("\\.");
            if(currVerson_array.length == 2){
                currVerson_array = new String[]{currVerson_array[0],currVerson_array[1],"0"};
            }
            if(lastVerson_array.length == 2){
                lastVerson_array = new String[]{lastVerson_array[0],lastVerson_array[1],"0"};
            }
            if(currVerson_array.length == 3 && lastVerson_array.length == 3){
                for(int i = 0;i<3;i++){
                    long curr = StringUtils.Convert2Long(currVerson_array[i]);
                    long last = StringUtils.Convert2Long(lastVerson_array[i]);
                    if(curr < last){
                        return true;
                    }else if(curr == last){
                        continue;
                    }else{
                        return false;
                    }
                }
            }else{
                //数据格式异常，则视为不需要更新
                return false;
            }
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }
}
